﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;
using System.IO;

namespace BookStore.admin
{
    public partial class Product : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
            if (!Page.IsPostBack)
            {
                LoadddlAuthor();
                LoadddlManu();
                LoadddlCatogory();
            }

        }
        public void showAll()
        {
            gvProduct.DataSource = productService.getAll();
            gvProduct.DataBind();
        }

        public void ResetControl() {
            txtProductid.Text = "";
            txtProductname.Text = "";
            txtPrice.Text = "";
            txtDesription.Text = "";
            txtQuantity.Text = "";
            txtDesription.Text = "";
            rdbSale.Checked = false;
            Image1.ImageUrl = "";
        }


        public void LoadddlCatogory() {
            ddlCatogoryid.DataSource = catogoryService.getAll();
            ddlCatogoryid.DataTextField = "catogryName";
            ddlCatogoryid.DataValueField = "catogoryID";
            ddlCatogoryid.DataBind();

        }

        
        public void LoadddlManu()
        {
            ddlManufactureid.DataSource = manufacturerService.getAll();
            ddlManufactureid.DataTextField = "manufacturerName";
            ddlManufactureid.DataValueField = "manufacturerID";
            ddlManufactureid.DataBind();
        }
        public void LoadddlAuthor()
        {
            ddlAuthorid.DataSource = authorService.getAll();
            ddlAuthorid.DataTextField = "authorName";
            ddlAuthorid.DataValueField = "authorID";
            ddlAuthorid.DataBind();
        }

        public void insertProduct()
        {
            productInfo objProduct = new productInfo();

            //txtProductid.Text = ddlCatogoryid.SelectedValue;
            //if (productService.checkProductID(txtProductid.Text))
            //{
            //    Response.Write("<script language='javascript'>alert('productID existed');</script>");
            //    return;
            //}
            
            objProduct.CatogoryID = Convert.ToInt32(ddlCatogoryid.SelectedValue);

            objProduct.productName = txtProductname.Text;

            objProduct.ManufacturerID = Convert.ToInt32(ddlManufactureid.SelectedValue);
            objProduct.price = float.Parse(txtPrice.Text);

           
            objProduct.manufacturerID = Convert.ToInt32(ddlManufactureid.SelectedValue);
            objProduct.price = float.Parse(txtPrice.Text);

            objProduct.quantity = Convert.ToInt32(txtQuantity.Text);
            objProduct.sale = rdbSale.Checked;

            objProduct.Date = DateTime.Now.Date;
            objProduct.Active = true;

            objProduct.description = txtDesription.Text;
            objProduct.authorID = Convert.ToInt32(ddlAuthorid.SelectedValue);
            objProduct.image = Image1.ImageUrl.ToString();
            
            bool test = productService.insertProduct(objProduct);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateProduct()
        {
            productInfo objProduct = new productInfo();

            txtProductid.Text = ddlCatogoryid.SelectedValue + "" + ddlCatogoryid.SelectedValue;
            objProduct.CatogoryID = Convert.ToInt32(ddlCatogoryid.SelectedValue.ToString());
            objProduct.productID =Convert.ToInt32( txtProductid.Text);

            
            objProduct.productID = Convert.ToInt32(txtProductid.Text);

            objProduct.productName = txtProductname.Text;

            objProduct.ManufacturerID = Convert.ToInt32(ddlManufactureid.SelectedValue);
            objProduct.price = float.Parse(txtPrice.Text);

            
            objProduct.manufacturerID = Convert.ToInt32(ddlManufactureid.SelectedValue);
            objProduct.price = float.Parse(txtPrice.Text);

            objProduct.quantity = Convert.ToInt32(txtQuantity.Text);
            objProduct.sale = rdbSale.Checked;
            objProduct.description = txtDesription.Text;
            objProduct.authorID = Convert.ToInt32(ddlAuthorid.SelectedValue);
            objProduct.image = Image1.ImageUrl.ToString();
            bool test = productService.updateProduct(objProduct);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
      

        protected void lbtAddT_Click1(object sender, EventArgs e)
        {
            
            flag = true;
            pnView.Visible = false;
            pnUpdate.Visible = true;
            ResetControl();
       
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            
            if (flag == true)
            {
                insertProduct();
            }
            else
            {
                updateProduct();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtRefreshT_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void gvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ProductID = Convert.ToInt32(gvProduct.Rows[e.RowIndex].Cells[0].Text);
            bool test = productService.deleteProduct(ProductID);
               
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvProduct_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int ProductID = Convert.ToInt32(gvProduct.Rows[e.NewSelectedIndex].Cells[0].Text);
            productInfo objProduct = productService.getbyID(ProductID);
            txtProductid.Text = objProduct.productID.ToString();
            ddlCatogoryid.SelectedValue = objProduct.CatogoryID.ToString();
            txtProductname.Text = objProduct.productName;



            ddlManufactureid.SelectedValue=objProduct.manufacturerID.ToString();
            txtPrice.Text = objProduct.price.ToString();
            txtQuantity.Text = objProduct.quantity.ToString();
            rdbSale.Checked=objProduct.sale;
            Image1.ImageUrl = objProduct.image;
            txtDesription.Text=objProduct.description;
            ddlAuthorid.SelectedValue=objProduct.authorID.ToString();
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }
        string tenFile = "";
        protected void btnLoad_Click(object sender, EventArgs e)
        {
            if (fulImg.HasFile && fulImg.FileName.Length > 0)
            {
                tenFile = Path.GetFileName(fulImg.PostedFile.FileName);
                string str = tenFile.Substring(tenFile.LastIndexOf("."));

                if (str.Equals(".jpg") || str.Equals(".png") || str.Equals(".jpeg"))
                {
                    fulImg.SaveAs(Server.MapPath("/image/") + tenFile);
                }
                tenFile = "/image/" + tenFile;
                Image1.ImageUrl = tenFile;
            }
            
        }


        protected void ddlCatogoryid_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadddlCatogory();
        }

        protected void ddlSubcatogoryid_SelectedIndexChanged(object sender, EventArgs e)
        {
            //lblsubCato.Text = ddlSubcatogoryid.SelectedValue.ToString();
        }

        protected void ddlManufactureid_SelectedIndexChanged(object sender, EventArgs e)
        {
            //lblManu.Text = ddlManufactureid.SelectedValue.ToString();
        }

        protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvProduct.PageIndex = e.NewPageIndex;
            showAll();
        }

    }
}
